<?php

global $WSC;

require_once(TANGRA_MAIN_DIR.'nls/date.inc.php');


require_once('tcms_vpt_page_wp.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/category/tcms_category_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/rss/rss_channel_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/vp_templates/tcms_vpt_article_page_wp.class.php');


class TCMS_VPT_RSS_News_WP extends TCMS_VPT_Page_WP {
	function __construct(TCMS_VPage_DBC $vpage, TCMS_VPage_Template_DBC $vpt) {
		parent::__construct($vpage, $vpt, 'tcms_vpt_rss_page');
	}

	public function create_view($name = 'default') {
		$ret = new TCMS_Web_Page_View($this, $name, $this->get_settings_visuals()->get_html_resource_path().'pages/');

		return $ret;
	}


	protected function default_event() {
		$dbc = $this->get_dbc();

		$cat_hid = current(TCMS_VPage_System_Attribute_DBC::load_all_by_sa_export_name_vpage($dbc, 'category_hid', $this->get_vpage()->get_id()));

		$category = new TCMS_Category_DBC();
		if ($category->load_by_hid($dbc, $cat_hid->get_value())) {
			$this->export('category', $category->get_title());
			$channel_sa = current(TCMS_VPage_System_Attribute_DBC::load_all_by_sa_export_name_vpage($dbc, 'rss_channel_id', $this->get_vpage()->get_id()));
			if ($channel_sa) {
				$rc = new RSS_Channel_DBC();

				if ($rc->load_by_id($dbc, $channel_sa->get_value())) {
					$this->export('rc', $rc);
					$this->export('feed_rel_path', $this->get_vpage()->get_path());

					$articles_arr = $this->select_vpages($dbc, $category->get_id(), 'TCMS_VPT_Article_Page_WP', 10);
					$this->prepare_for_export_articles($articles_arr);
					$this->export('items', $articles_arr);

					if ($articles_arr) {
						$last_item = $articles_arr[0];
						$pubDate_raw = $articles_arr[0]['ci']['created'];
						$timestamp = datetime_to_timestamp($pubDate_raw);
						$pubDate = gmdate('D, d M Y H:i:s', $timestamp) . ' GMT';
					} else {
						$pubDate = gmdate('D, d M Y H:i:s') . ' GMT';
					}
					$this->export('pubdate', $pubDate);

					$last_build_date = gmdate('D, d M Y H:i:s') . ' GMT';
					$this->export('last_build_date', $last_build_date);
				}
			}
		}

		return parent::default_event();
	}


	public static function get_short_text_description(DB_Connection $dbc, $vpage_id) {
		tangra_if_not_int_throw_e($vpage_id);

		$channel_sa = current(TCMS_VPage_System_Attribute_DBC::load_all_by_sa_export_name_vpage($dbc, 'rss_channel_id', $vpage_id));

		$rc = new RSS_Channel_DBC();
		if ($channel_sa) {
			if ($rc->load_by_id($dbc, $channel_sa->get_value())) {
				$ret = $rc->get_title();
			} else {
				$ret = '';
			}
		} else {
			$ret = '';
		}

		return $ret;
	}


	public function select_vpages(DB_Connection $dbc, $category_id, $vpt_class, $count, $offset = 0) {
		tangra_if_not_int_throw_e($category_id);
		$vpt_class = addslashes($vpt_class);

		$sql = "select vpages.id, vpages.path ".
						"from tcms_vpages vpages, ".
								"tcms_vpage_templates vpt, ".
								"tcms_vpage_categories vp_cats, ".
								"tcms_vpage_cis vp_cis, ".
								"tcms_cis cis ".
						"where vp_cats.category = $category_id and ".
								"vp_cats.vpage = vpages.id and ".
								"vpages.online = 1 and ".
								"vpages.template = vpt.id and ".
								"vpt.wp_class_name = '$vpt_class' and ".
								"vpages.id = vp_cis.vpage and ".
								"vp_cis.ci_id =  cis.id ".
						"group by vpages.id, vpages.path ".
						"order by cis.created desc";


		$rez = $dbc->select_limit($sql, $count, $offset);
		$ret = array();
		while ($rez_obj = $rez->fetch_object()) {
			$ret[] = array('vpage' => $rez_obj->ID, 'path' => $rez_obj->PATH);
		}

		return $ret;
	}


	private function prepare_for_export_articles(&$vpages_arr) {
		$dbc = $this->get_dbc();
		$hidden_inc = $this->_config->get_hidden_dir().'inc/';

		foreach($vpages_arr as $key => $vpage) {
			$article = TCMS_VPT_Article_Page_WP::get_article_ci_for_page($dbc, $vpage['vpage']);
			$vpages_arr[$key]['ci']['title'] = strip_tags($article->get_title()->get_value());
			$vpages_arr[$key]['ci']['intro'] = $article->get_intro()->get_value();
			$vpages_arr[$key]['ci']['images'] = TCMS_VPT_Page_WP::load_images_for_ci($dbc, $article->get_id());
			$vpages_arr[$key]['ci']['attachments'] = TCMS_VPT_Page_WP::load_attachments_for_ci($dbc, $article->get_id());

			$timestamp = datetime_to_timestamp($article->get_ci()->get_created());
			$gmt_mtime = gmdate('D, d M Y H:i:s', $timestamp) . ' GMT';

			$vpages_arr[$key]['ci']['created'] = $gmt_mtime;
		}
	}
}